package Qverst::CSS::Adaptor::Simple;
use Moose;
use Carp qw(croak confess);

sub output {
    my ($self , $document) = @_;
    
    my $res;
    my $list = $document->list;
    
    foreach my $property (@$list) {
	my $selector = $property->selector;
	$res .= join " , " , map { $_->css } @$selector;
	$res .= " {\n";
		
	my $style = $property->list;
	$res .= join "\n" , map { $self->style_css($_) } @$style;
	$res .= "\n}\n";
    }
    
    return $res;
}

sub style_css {
    my ($self , $style) = @_;
    
    my $res = "\t" . $style->name . ": ";
    
    if ($style->value) {
	if ($style->value->can('value')) {
	    if ($style->value->can('css')) {
		$res .= $style->value->css;
	    } else {
		$res .= $style->value->value;
            }
	} elsif ($style->value->can('inherit')) {
	    $res .= "inherit";
	} else {
	    croak "Error\n";
        }
    } else {
	my @val = ();
	my $list = $style->list;
	foreach my $p (@$list) {
	    if ($p->value->can('css')) {
		push @val , $p->value->css;
	    } else {
		push @val , $p->value->value;
	    }
	}
	$res .= join " " , @val;
    }
    
    if ($style->important) {
	$res .= " !important";
    }
    
    return $res . ";";
}

1;

=head1 NAME

CSS::Adaptor::Simple - Create nice output of CSS::Builder object

=head1 SYNOPSIS

  use CSS;

  # create a CSS object with an adaptor
  my $css = new CSS({
       'adaptor' => 'CSS::Adaptor',
  });


  # load some CSS data
  $css->read_file( "my_file.css" );


  # change the adaptor
  $css->set_adaptor( "CSS::Adaptor::Pretty" );


  # output CSS object using the current adaptor
  print $css->output();
  

=head1 DESCRIPTION

This class is used by CSS to translate a CSS object to a string. This 
allows CSS data to be easily mapped into other formats.

This documentation is for people who want to write their own CSS::Adaptor
module. For usage information, see the documentation for CSS.

=head1 METHODS

=head2 CONSTRUCTOR

=over 4

=item C<new()>

Called without options.

=back

=head2 FUNCTIONS

=over 4

=item C<output_rule( $rule )>

returns a string containing a formatted CSS::Style object, passed as an object ref

=item C<output_selectors( $selectors )>

returns a string containing a formatted list of CSS::Selector objects, passed as an array ref

=item C<output_properties( $properties )>

returns a string containing a formatted list of CSS::Property objects, passed as an array ref

=item C<output_values( $values )>

returns a string containing a formatted list of CSS::Value objects, passed as an array ref

=back

=head1 AUTHORS

Copyright (C) 2001-2002, Allen Day <allenday@ucla.edu>

Copyright (C) 2003-2004, Cal Henderson <cal@iamcal.com>

=head1 SEE ALSO

L<CSS>

=cut
